package org.zk.dubbo_consumer.controller;


import com.github.pagehelper.PageInfo;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zk.dubbo_consumer.util.YjlMapIntert;
import org.zk.entity.Teacher;
import org.zk.entity.TeacherBill;
import org.zk.service.AdminBillService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@Controller
@RequestMapping("/adminBill")
public class AdminBillController {

    @Resource
    private AdminBillService adminBillService;
    @Resource
    private YjlMapIntert yjlMapIntert;


    /**
     * 姚家乐
     * 2019-12-15 12:58
     * 指向member-list页面
     * @return
     */
    @RequestMapping("/togetTeacherBill")
    public String togetTeacherBill(){
        return "admin/member-list";
    }

    /**
     * 姚家乐
     * 2019-12-15 2:09
     * 遍历查询所有老师和财务相关的信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTeacherBill")
    public Map getTeacherBill(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize, String seetext){
        Map<String,Object> map = new HashMap<>();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("seetext",seetext);

        PageInfo<Teacher> pageInfo = adminBillService.getTeacherBillPage(map);
        map.clear();
        map.put("page",pageInfo);
        map.put("pages",pageInfo.getPages());
        map.put("total",pageInfo.getTotal());
        return map;
    }

    /**
     * 姚家乐
     * 2019-12-16 4:17
     * 通过老师的ID查看老师的详细的流水账
     * @param teacherid
     * @return
     */
    @RequestMapping("/getTeacherBillByTeacherid/{teacherid}")
    public String getTeacherBillByTeacherid(Model model, @PathVariable("teacherid") Integer teacherid){
        Map<String,Object> map = adminBillService.getCountOrderByTeacherid(teacherid);
        model.addAttribute("map",map);
        model.addAttribute("teacherid",teacherid);
        return "admin/member-del";
    }

    /**
     * 姚家乐
     * 2019-12-25 3:48
     * 通过老师的ID查看老师的详细的流水账的第二步，分页
     * @param pageNum
     * @param pageSize
     * @param teacherid
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTeacherBillByTeacherid0")
    public Map<String ,Object> getTeacherBillByTeacherid0(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,int teacherid,String datemin,String datemax) throws ParseException {
        Map<String,Object> map = new HashMap<>();
        if (!datemax.equals("") && !datemin.equals("")){
            map.put("start",datemin);
            map.put("end",datemax);
        }

        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("teacherid",teacherid);

        PageInfo<TeacherBill> pageInfo = adminBillService.getTeacherbillByTeacherid0Page(map);
        map.clear();
        map.put("page",pageInfo);
        map.put("pages",pageInfo.getPages());//总页数
        map.put("total",pageInfo.getTotal());//总条数
        return map;
    }



    /**
     * 姚家乐
     * 2019-12-17  10:56
     * 指向工资待发放页面
     * @return
     */
    @RequestMapping("/togetPayoffToTeacher")
    public String togetPayoffToTeacher(){
        return "admin/member-record-browse";
    }

    /**
     * 姚家乐
     * 2019-12-18 11:43
     * 管理员在合适的时间点击向教师账单表中插入条件月需发工资的数据信息
     * @return
     */
    @ResponseBody
    @RequestMapping("/getInsertTeacherBill")
    public Boolean getInsertTeacherBill(){
        Boolean t = adminBillService.insertTeacherBill();
        return t;
    }

    /**
     * 姚家乐
     * 2019-12-17 11:17
     * 遍历出未发工资的教师,以及应发工资的金额
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/getPayoffToTeacher")
    public Map getPayoffToTeacher(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize, String seetext){
        Map<String,Object> map = new HashMap<>();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("seetext",seetext);

        PageInfo<TeacherBill> pageInfo = adminBillService.getPayoffToTeacherPage(map);
        map.clear();
        map.put("page",pageInfo);
        map.put("pages",pageInfo.getPages());
        map.put("total",pageInfo.getTotal());

        return map;
    }

    /**
     * 姚家乐
     * 2019-12-18 9:28
     * 给教师钱包转账后的返回的false或true
     * @return
     */
    @ResponseBody
    @RequestMapping("/getTransfer")
    public Boolean getTransfer(HttpSession session, int teacherid, double teachermoney, double price){
        /*int adminid = (int) session.getAttribute("adminId");*/
        int amdinid = 2;
        return adminBillService.updateTeacherfer(amdinid,teacherid,teachermoney,price);
    }

    /**
     * LYT 平台支出账单记录（给老师转账）模糊查询、分页
     * @return
     */
    @RequestMapping("toOutAdminBill")
    public ModelAndView getOutList(){
        return new ModelAndView("/admin/adminOutBill");
    }
    @ResponseBody
    @RequestMapping("/getOutAdminBill")
    public Map getOutAdminBill(int pageSize, int pageNum, HttpSession session,String start,String end){
        Map map=new HashMap();
        if(!start.equals("")){
            start+= " 00:00:00";
        }
        if(!end.equals("")){
            end+=" 23:59:59";
        }
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("start",start);
        map.put("end",end);
        PageInfo page=adminBillService.getOutPage(map) ;
        session.setAttribute("page",page);
        map.put("page",page);
        map.put("pages",page.getTotal());
        map.put("totalPage",page.getPages());//总页数=总条数/当前显示条数
        return map;
    }

    /**
     * LYT 学生给平台付款记录（收支）模糊查询、分页
     * @return
     */
    @RequestMapping("toInAdminBill")
    public ModelAndView getInList(){
        return new ModelAndView("/admin/adminInBill");
    }

    @ResponseBody
    @RequestMapping("/getInAdminBill")
    public Map getInAdminBill(int pageSize, int pageNum, HttpSession session,String start,String end){
        Map map=new HashMap();
        if(!start.equals("")){
            start+= " 00:00:00";
        }
        if(!end.equals("")){
            end+=" 23:59:59";
        }
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("start",start);
        map.put("end",end);
        PageInfo page=adminBillService.getInPage(map) ;
        session.setAttribute("page",page);
        map.put("page",page);
        map.put("pages",page.getTotal());
        map.put("totalPage",page.getPages());//总页数=总条数/当前显示条数
        return map;
    }

    /**
     * LYT 管理员删除支出记录（给老师转账记录）
     * @param id
     * @return
     */
    @RequestMapping("/delOutAdminBill")
    @ResponseBody
    public Boolean delOutAdminBill(int id){
        adminBillService.delOutAdminBill(id);
        return  true;


    }

    /**
     * LYT  管理员删除收入记录（学生的付款记录）
     */
    @RequestMapping("/delInAdminBill")
    @ResponseBody
    public Boolean delInAdminBill(int id){
        adminBillService.delInAdminBill(id);
        return  true;
    }


}

